<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="success" v-on:click="onRefresh">同步</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="warning" @click="entryShowFlag = !entryShowFlag">
                                {{ entryShowFlag ? '隐藏入口图' : '显示入口图' }}
                            </el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getData">查询</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="50">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="pictureId"
                label="图片ID">
            </el-table-column>
            <el-table-column
                prop="clickRate"
                label="点击率">
            </el-table-column>
            <el-table-column
                prop="pictureUrl"
                v-if="entryShowFlag"
                label="图片链接">
                <template v-slot="scope">
                    <el-image style="width: 80px; height: 80px"
                              :preview-src-list="picUrls"
                              :src="scope.row.pictureUrl"
                              fit="fill">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                prop="onlineTime"
                label="上线时间">
            </el-table-column>
            <el-table-column
                prop="offlineTime"
                label="下线时间">
            </el-table-column>
            <el-table-column
                prop="coverWebpUrl"
                v-if="entryShowFlag"
                label="WEBP链接">
                <template v-slot="scope">
                    <el-image v-if="scope.row.coverWebpUrl"
                              style="width: 80px; height: 80px"
                              :preview-src-list="picUrls"
                              :src="scope.row.coverWebpUrl"
                              fit="fill">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                prop="coverGifUrl"
                v-if="entryShowFlag"
                label="GIF链接">
                <template v-slot="scope">
                    <el-image v-if="scope.row.coverGifUrl"
                              style="width: 80px; height: 80px"
                              :preview-src-list="picUrls"
                              :src="scope.row.coverGifUrl"
                              fit="fill">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[20, 40, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" size="small">
                <el-form-item label="门店ID">
                    <el-input v-model="addOrUpdateForm.shopId"
                              placeholder="门店ID"></el-input>
                </el-form-item>
                <el-form-item label="位置ID">
                    <el-input v-model="addOrUpdateForm.poiId"
                              placeholder="位置ID"></el-input>
                </el-form-item>
                <el-form-item label="图片ID">
                    <el-input v-model="addOrUpdateForm.pictureId"
                              placeholder="图片ID"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="点击率">
                    <el-input v-model="addOrUpdateForm.clickRate"
                              placeholder="点击率"></el-input>
                </el-form-item>
                <el-form-item label="图片链接">
                    <el-input v-model="addOrUpdateForm.pictureUrl"
                              placeholder="图片链接"></el-input>
                </el-form-item>
                <el-form-item label="上线时间">
                    <el-date-picker
                        v-model="addOrUpdateForm.onlineTime"
                        type="datetime"
                        placeholder="上线时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="下线时间">
                    <el-date-picker
                        v-model="addOrUpdateForm.offlineTime"
                        type="datetime"
                        placeholder="下线时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="webp链接">
                    <el-input v-model="addOrUpdateForm.coverWebpUrl"
                              placeholder="webp链接"></el-input>
                </el-form-item>
                <el-form-item label="gif链接">
                    <el-input v-model="addOrUpdateForm.gifWebpUrl"
                              placeholder="gif链接"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addDpEntryPicHistory,
    deleteDpEntryPicHistory,
    getDpEntryPicHistory,
    getDpEntryPicHistoryByPage,
    updateDpEntryPicHistory
} from '@/api/api'

export default {
    name: "DpEntryPicHistory",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 20,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},

            // 图片数组
            picUrls: [],

            // 默认不显示
            entryShowFlag: false,
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        shopId: String,
        shopName: String,
        size: {
            type: String,
            default: 'small'
        },
    },
    watch: {
        shopName: function (val, oldVal) {
            this.shopName = val;
        }
    },
    methods: {
        onRefresh() {
            getDpEntryPicHistory({shopId: this.shopId}).then(data => {
                if (data.respCode == 1000) {
                    this.getData();
                }
            })
        },
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },

        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addDpEntryPicHistory(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateDpEntryPicHistory(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteDpEntryPicHistory({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            if (this.shopId) {
                this.condition.search.shopId = this.shopId;
            }
            getDpEntryPicHistoryByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
                if (this.tableData) {
                    this.tableData.forEach((item, index, arr) => {
                        if (item.pictureUrl) {
                            this.picUrls.push(item.pictureUrl);
                        }
                        if (item.coverWebpUrl) {
                            this.picUrls.push(item.coverWebpUrl);
                        }
                        if (item.coverGifUrl) {
                            this.picUrls.push(item.coverGifUrl);
                        }
                    })
                }
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
